$(function () {
	$("#jqGrid").jqGrid({
		url: '../firmwarezip/list',
		datatype: "json",
		colModel: [
			{label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true},
			{label: '唯一标识', name: 'rowId', index: 'row_id', width: 80, hidden: true},
			{label: '硬件类型', name: 'hardwareType', index: 'hardware_type', width: 80},
			{label: '固件型号', name: 'hardwareModel', index: 'hardware_model', width: 80},
			{label: '固件版本', name: 'firmwareLatestVersionCode', index: 'firmware_latest_version_code', width: 80},
			{label: '产品型号', name: 'equipmentModel', index: 'equipment_model', width: 80},
			{
				label: '下载地址',
				name: 'firmwareDownloadAddr',
				index: 'firmware_download_addr',
				width: 80,
				formatter: function (cellvalue, options, rowObject) {
					return '<a href="http://gcsbucket.oss-cn-hongkong.aliyuncs.com/' + cellvalue + '"  target="_blank">下载</a>';
				}
			},
			{label: '上传时间', name: 'addTime', index: 'add_time', width: 80},
			{label: '固件描述信息', name: 'firmwareDescription', index: 'firmware_description', width: 80, hidden: true},
			{
				label: '船体升级模式）',
				name: 'isMandatoryUpgrade',
				index: 'is_mandatory_upgrade',
				width: 80,
				formatter: function (value) {
					var result = "";
					if (value == 0) {
						result = '<span class="label label-success">不强制</span>';
					}
					if (value == 1) {
						result = '<span class="label label-danger">强制</span>';
					}
					return result;
				}
			},
			{label: '合并版本号', name: 'mergeVersion', index: 'merge_version', width: 80},
			{
				label: 'PWV4遥控更新',
				name: 'updateMethod',
				index: 'update_method',
				width: 80,
				formatter: function (value) {
					var result = "";
					if (value == 0) {
						result = '<span class="label label-danger">强制</span>';
					}
					if (value == 1) {
						result = '<span class="label label-success">不强制</span>';
					}
					return result;
				},
			},
			{
				label: '状态', name: 'status', index: 'status', width: 80, formatter: function (value) {
					var result = "";
					if (value == 0) {
						result = '<span class="label label-success">启用</span>';
					}
					if (value == 1) {
						result = '<span class="label label-danger">停用</span>';
					}
					return result;
				},
			},
			{label: '固件大小', name: 'firmwareSize', index: 'firmware_size', width: 80, hidden: true},
			{label: '添加人', name: 'addUser', index: 'add_user', width: 80, hidden: true}
		],
		viewrecords: true,
		height: 385,
		rowNum: 10,
		rowList: [10, 30, 50],
		rownumbers: true,
		rownumWidth: 25,
		autowidth: true,
		multiselect: true,
		pager: "#jqGridPager",
		jsonReader: {
			root: "page.list",
			page: "page.currPage",
			total: "page.totalPage",
			records: "page.totalCount"
		},
		prmNames: {
			page: "page",
			rows: "limit",
			order: "order"
		},
		gridComplete: function () {
			//隐藏grid底部滚动条
			$("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
		}
	});


});



$(document).ready(function () {
	new AjaxUpload('#upload', {
		action: '../sys/oss/upload',
		name: 'file',
		autoSubmit: true,
		responseType: "json",
		onSubmit: function (file, extension) {
			// if (!(extension && /^(jpg|jpeg|png|gif|bin|apk|zip|conf|patch)$/.test(extension.toLowerCase()))) {
			//     alert('只支持jpg、png、gif、bin格式的文件！');
			//     return false;
			// }
		},
		onComplete: function (file, r) {
			if (r.code == 0) {
				$("#downloadPath").val(r.url);
				$("#fileId").val(r.fileKey);
				alert('文件更新成功！');
			} else {
				alert(r.msg);
			}
		}
	});
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		firmwareZip: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.firmwareZip = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var zhDescription = $("#zhDescription").val();
			var enDescription = $("#enDescription").val();
			var jaDescription = $("#jaDescription").val();
			vm.firmwareZip.firmwareDownloadAddr = $("#downloadPath").val();
			var isMandatoryUpgradeSelect = $("#isMandatoryUpgradeSelect").val();
			var isUseTo = $("#isUseTo").val();
			var isUpdateMethod = $("#isUpdateMethod").val();
			vm.firmwareZip.firmwareDescription = '{"zh":"' + zhDescription + '","en":"' + enDescription + '","ja":"' + jaDescription + '"}';
			vm.firmwareZip.isMandatoryUpgrade = isMandatoryUpgradeSelect;
			vm.firmwareZip.updateMethod = isUpdateMethod;
			vm.firmwareZip.rowId = $("#fileId").val();
			vm.firmwareZip.status = isUseTo;
			console.log(vm.firmware);
			var url = vm.firmwareZip.id == null ? "../firmwarezip/save" : "../firmwarezip/update";
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.firmwareZip),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../firmwarezip/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../firmwarezip/info/"+id, function(r){
                vm.firmwareZip = r.firmwareZip;
				console.log(r.firmwareZip.firmwareDescription);
				var firmwareDescription = JSON.parse(r.firmwareZip.firmwareDescription);
				$("#zhDescription").val(firmwareDescription['zh']);
				$("#enDescription").val(firmwareDescription['en']);
				$("#jaDescription").val(firmwareDescription['ja']);
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});